package com.example.hm_dtfkz.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.DQLog;
import com.android.base.util.DisplayUtil;
import com.android.base.util.ToastUtil;
import com.dq.ad.AdManager;
import com.dq.ad.UMManager;
import com.example.hm_dtfkz.R;
import com.example.hm_dtfkz.config.AppConfig;
import com.example.hm_dtfkz.databinding.DialogLuckyCircleBinding;
import com.example.hm_dtfkz.entity.QALuckyConfigResp;
import com.example.hm_dtfkz.entity.QAPrize;
import com.example.hm_dtfkz.ui.view.QALuckyView;
import com.example.hm_dtfkz.util.sound.MediaBackManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * FileName: DialogLuckyCircle
 * Author: admin
 * Date: 2021/11/5 20:00
 * Description:
 */
public class DialogLuckyCircle extends Dialog implements QALuckyView.OnRotationListener {

    private DialogLuckyCircleBinding luckyBinding;
    private final Activity activity;
    private List<QAPrize> prizeList;
    private QAPrize qaPrize;
    private int luckyTimes=3;
    /**
     * 抽中奖品类型
     */
    private int prizeType;
    /**
     * 抽中红包的金额
     */
    private double luckyCash;
    /**
     * 抽中奖品位置
     */
    private int luckyPos = 0;
    /**
     * 抽中奖品位置
     */
    private int luckyIndex = 0;

    private boolean isReward = false;
    private OnLuckAnimationEndListener mLuckAnimationEndListener;

    public DialogLuckyCircle(@NonNull Activity a) {
        super(a, R.style.dialog_loading);
        this.activity = a;
    }


    public DialogLuckyCircle setOnLuckAnimationEndListener(OnLuckAnimationEndListener listener) {
        this.mLuckAnimationEndListener = listener;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        luckyBinding = DialogLuckyCircleBinding.inflate(getLayoutInflater());
        setContentView(luckyBinding.getRoot());

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = DisplayUtil.getScreenWidth(activity);
        lp.height = DisplayUtil.getScreenHeight(activity);
        getWindow().setAttributes(lp);
        getWindow().setWindowAnimations(R.style.dialog_answer_correct);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView() {
        UMManager.onEvent(UMManager.Event.luckdraw_click);
        luckyBinding.luckyView.setOnRotationListener(this);
        luckyBinding.ivBtBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityClickUtil.getInstance().isFastClick()) {
                    //防止用户快速点击
                    return;
                }

                if (AppConfig.TD_BANNED == 1) {
                    ToastUtil.showShortToast(activity, AppConfig.TD_BANNED_MSG);
                    return;
                }

                if (luckyTimes <= 0) {
                    ToastUtil.showShortToast(activity, "今日次数已领完");
                    return;
                }

                if (AppConfig.hasNoAd()) {
                    AppConfig.showNoAdToast(activity);
                } else {
                    luckyBinding.luckyView.startRotatingDelay(5000);
                    luckyBinding.luckyView.setStopPosition(2);
//                    luckyBinding.ivBtBg.setEnabled(false);
                    if (AppConfig.getBackMusic()) {
                        MediaBackManager.getInstance(activity).pause();
                    }
                    isReward = false;

//                    AdManager.getInstance().showRewardVideoAD("", activity, new AdManager.OnRewardVideoADListener() {
//                        @Override
//                        public void onClosed(String orderId) {
//                            if (AppConfig.getBackMusic()) {
//                                MediaBackManager.getInstance(activity).start();
//                            }
//                            if (isReward) {
//                                UMManager.onEvent(UMManager.Event.luckdraw_open);
//                                lucky();
//                                luckyBinding.btnClose.setEnabled(false);
//                            } else {
//                                ToastUtil.showShortToast(activity, "广告未观看完成，抽奖失败");
//                            }
//                        }
//
//                        @Override
//                        public void onReward() {
//                            isReward = true;
//                        }
//
//                        @Override
//                        public void onShow(double ecpm) {
//
//                        }
//
//                        @Override
//                        public void onFailed(int i, AdError adError) {
//                            if (AppConfig.getBackMusic()) {
//                                MediaBackManager.getInstance(activity).start();
//                            }
//                            luckyBinding.ivBtBg.setEnabled(true);
////                            ToastUtil.showShortToast(activity, "广告加载失败，抽奖失败");
//                        }
//
//                        @Override
//                        public void onTimeout() {
//                            if (AppConfig.getBackMusic()) {
//                                MediaBackManager.getInstance(activity).start();
//                            }
//                            luckyBinding.ivBtBg.setEnabled(true);
//                        }
//                    });
                }
            }
        });

        luckyBinding.btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        getLuckyConfig();

        AdManager.getInstance().showInterstitialAD(activity);
    }

    private void getLuckyConfig() {
//        HXCurrencySDK.getInstance().getQALuckyConfig(new HttpUtil.HttpUtilCallback() {
//            @Override
//            public void onSuccess(int i, String response) {
//                DQLog.e("奖盘配置信息获取成功：" + response);
//                QALuckyConfigResp qaLuckyConfigResp = JsonUtil.parseObject(response, QALuckyConfigResp.class);
//                if (qaLuckyConfigResp != null) {
//                    if (qaLuckyConfigResp.getErrorCode() == 0) {
        QALuckyConfigResp qaLuckyConfigResp=new QALuckyConfigResp();
                        updateUI(qaLuckyConfigResp);
//                    } else {
//                        DQLog.e("获取抽奖配置异常：" + qaLuckyConfigResp.getErrorMessage());
//                        ToastUtil.showShortToast(activity, "获取抽奖配置异常：" + qaLuckyConfigResp.getErrorMessage());
//                    }
//                } else {
//                    DQLog.e("获取抽奖配置异常");
//                    ToastUtil.showShortToast(activity, "抽奖数据未配置");
//                }
//            }
//
//            @Override
//            public void onFailed(int i, Throwable throwable) {
//                DQLog.e("奖盘配置信息获取异常：" + throwable.toString());
//                ToastUtil.showShortToast(activity, "网络错误");
//            }
//        });
    }

//    private void lucky() {
//        HXCurrencySDK.getInstance().qaLuckyReq(new HttpUtil.HttpUtilCallback() {
//            @Override
//            public void onSuccess(int i, String response) {
//                DQLog.e("开始抽奖信息获取成功：" + response);
//                QALuckyResp qaLuckyResp = JsonUtil.parseObject(response, QALuckyResp.class);
//                if (qaLuckyResp != null) {
//                    if (qaLuckyResp.getErrorCode() == 0) {
//                        updateUI2(qaLuckyResp);
//                    } else {
//                        DQLog.e("抽奖结果异常：" + qaLuckyResp.getErrorMessage());
//                        ToastUtil.showShortToast(activity, "抽奖结果异常：" + qaLuckyResp.getErrorMessage());
//                    }
//                } else {
//                    DQLog.e("抽奖结果异常");
//                    ToastUtil.showShortToast(activity, "抽奖失败");
//                }
//            }
//
//            @Override
//            public void onFailed(int i, Throwable throwable) {
//                DQLog.e("开始抽奖获取异常：" + throwable.toString());
//                ToastUtil.showShortToast(activity, "网络错误");
//            }
//        });
//    }

    private void updateUI(QALuckyConfigResp qaLuckyConfigResp) {
        prizeList = qaLuckyConfigResp.getPl();
        if (null == prizeList || prizeList.size() != 8) {
            if (!activity.isFinishing()) {
                ToastUtil.showShortToast(activity, "获取抽奖配置信息失败");
            }
            return;
        }
        luckyTimes = qaLuckyConfigResp.getPt();
        luckyBinding.tvLeftNum.setText("今日还剩余" + luckyTimes + "次");
        final int size = prizeList.size();
        luckyBinding.luckyView.setmItemCount(size);
        final String[] plist = new String[size];
        final List<Bitmap> bitmapList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            final int x = i;
            QAPrize qaPrize = prizeList.get(i);
            plist[i] = qaPrize.getPn();

            if (!activity.isFinishing()) {
//                HXADTool.getURLImageBitmapAsync(activity, qaPrize.getPi(), new HXADTool.Task.InterfaceIMG() {
//                    @Override
//                    public void result(Bitmap bitmap) {
//                        bitmapList.add(x, bitmap);
//                        if (bitmapList.size() == size) {
//                            luckyBinding.luckyView.setBitmapList(bitmapList);
//                        }
//                    }
//                });
            }
        }
        luckyBinding.luckyView.setTextList(Arrays.asList(plist));
    }

//    private void updateUI2(QALuckyResp qaLuckyResp) {
//        double pv = 0;
//        for (int index = 0; index < prizeList.size(); index++) {
//            QAPrize qaPrize = prizeList.get(index);
//            if (qaPrize.getPid() == qaLuckyResp.getPid()) {
//                DQLog.e("index:" + index);
//                this.qaPrize = qaPrize;
//                luckyPos = index;
//                luckyIndex = index;
//                pv = qaPrize.getPv();
//                break;
//            }
//        }
//        //奖品类型
//        prizeType = qaLuckyResp.getPt();
//
//        luckyCash = pv;
//        LoginConfig.setUserCash(LoginConfig.getUserCash() + luckyCash);
//        if (null != qaPrize) {
//            DQLog.e("pos:" + luckyPos + "抽到的奖品是：" + qaPrize);
//        }
//        luckyBinding.luckyView.setStopPosition(luckyIndex);
//        luckyBinding.luckyView.startRotation();
//    }

    @Override
    public void onStop(int currentItem) {
        luckyBinding.ivBtBg.setEnabled(true);
        luckyBinding.btnClose.setEnabled(true);
        mLuckAnimationEndListener.onLuckAnimationEnd(
                this,
                qaPrize,
                prizeType, luckyCash
        );
    }

    /**
     * 用于抽奖结果回调
     */
    public interface OnLuckAnimationEndListener {
        void onLuckAnimationEnd(Dialog dialog, QAPrize qaPrize, int prizeType, double money);
    }
}
